var mainPanel;
var chartPanel;
var dateFrom;
var dateTo;
var chartMap;
var chartImg;

if (Ext.isIE) {
	document
			.write('<style type="text/css">.x-form-field-wrap .x-form-date-trigger { margin-top:-1px  } </style>');
	document
			.write('<style type="text/css">.x-form-field-wrap .x-form-trigger { margin-top:-1px  } </style>');
}

function getDateField(id, name, format, defaultValue) {
	var date = new Ext.form.DateField({
		id : id,
		name : name,
		format : format,
		hideLabel : true,
		value : defaultValue
	});

	return date;
}

function getChartImg() {
	var conn = Ext.lib.Ajax.getConnectionObject().conn;
	var url = '../BuildChart?chartName=userStatusCount&chartType=bar&dateFrom='
			+ dateFrom.value + '&dateTo=' + dateTo.value + '&flag='
			+ Math.random();

	// Gets chart
	conn.open("GET", url, false);
	conn.send(null);
	var responseText = conn.responseText;
	if (!responseCheck(responseText))
		return;
	var chartMapEndIndex = responseText.indexOf("</map>");
	chartMap = responseText.substr(0, chartMapEndIndex + 6);
	chartImg = responseText.substr(responseText.indexOf("/",
			chartMapEndIndex + 6));
}

function responseCheck(responsetext) {
	if (responsetext == 'Error: Not login') {
		Ext.Msg.alert(TXT_common_hint, TXT_common_relogin, function() {
			window.location.href = '../eni/common/logout.jsp';
		});
		return false;
	}

	if (responsetext == 'Error: Site interface incorrectly') {
		Ext.Msg.alert(TXT_common_hint, TXT_SITE_CONFIGURATION_CHECK);
		return false;
	}

	return true;
}

function exportExcelButtonClick() {
	if (!BestvSearch.isSessionTimeOut()) {
		window.location.href = '../DispatchServlet?cmd=userStatusCommand&action=createExcelForUserStatus&expExcel=y&dateFrom='
				+ dateFrom.value
				+ '&dateTo='
				+ dateTo.value
				+ '&flag='
				+ Math.random();
	} else {
		Ext.Msg.alert(TXT_common_hint, TXT_common_relogin, function() {
			window.location.href = '../eni/common/logout.jsp';
		});
	}
}

function searchButtonClick() {
	if (dateFrom.value > dateTo.value) {
		Ext.Msg.alert(TXT_common_hint, TXT_search_month_check_msg);
		return;
	}

	// Gets chart image from server
	getChartImg();

	// Updates chart image to page
	var chartMapDiv = document.getElementById('chartMap');
	chartMapDiv.innerHTML = chartMap;

	var chartImgObj = document.getElementById('chartImg');
	chartImgObj.src = chartImg;

	chartPanel.doLayout();
}

function displayNewUserCountChart() {
	var today = new Date();
	var tenDaysAgo = today.add(Date.DAY, -10);
	dateFrom = getDateField('dateFrom', 'dateFrom', 'Y-m-d', tenDaysAgo
			.format('Y-m-d'));
	dateTo = getDateField('dateTo', 'dateFrom', 'Y-m-d', today.format('Y-m-d'));

	var searchButton = new Ext.Button({
		id : "searchBtn",
		text : TXT_common_search,
		type : "button",
		iconCls : 'icoInspect',
		handler : searchButtonClick
	});
	var exportExcelButton = new Ext.Button({
		id : "exportExcelButton",
		text : TXT_search_export_excel_text,
		type : "button",
		handler : exportExcelButtonClick
	});

	// Gets chart image from server
	getChartImg();

	chartPanel = new Ext.Panel(
			{
				border : false,
				region : 'center',
				align : 'center',
				bodyStyle : 'background:#FFF',
				height : 500,
				tbar : [ exportExcelButton, '->', '&nbsp;&nbsp;日期范围:',
						dateFrom, '~', dateTo, '-', searchButton, '-' ],
				items : {
					id : "chartPanelImg",
					html : '<div align="center" height ="100%" bgcolor="#FFFFFF"><div id="chartMap">'
							+ chartMap
							+ '</div>'
							+ '<img id="chartImg" src="'
							+ chartImg
							+ '"  usemap ="#chartToolTipMap"/></div>',
					border : false,
					style : "margin-top:5px;margin-left:0px;"
				}
			});

	mainPanel = new Ext.TabPanel({
		region : 'center',
		activeTab : 0,
		frame : true,
		buttonAlign : 'right',
		layoutOnTabChange : true,
		items : [ {
			id : 'dailyPanel',
			title : TXT_search_userStatusCount_label,
			closable : false,
			autoScroll : true,
			items : [ chartPanel ]
		} ]
	});

	BestvSearch.glb_mainPanel.add(mainPanel);
	BestvSearch.drawMainFrame(TXT_app_name + '-'
			+ TXT_search_userStatusCount_label, null);
}

Ext.onReady(displayNewUserCountChart);